/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tcpserver;

/**
 *
 * @author makkalot
 */
import java.awt.AWTException;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import udpserver.SaveImage;


public class TestServer extends GeneralTcpServer{

    //sonra dur
    private int send_status = 0; 
    private SaveImage save_video;
    private SystemTrayMan trayman;
    private String currentMesage = "none";
    
    public TestServer() throws IOException{
        
            super();
        try {
            trayman = new SystemTrayMan();
        } catch (AWTException ex) {
            Logger.getLogger(TestServer.class.getName()).log(Level.SEVERE, null, ex);
        }
  }
    
    @Override
    public String handleData(byte[] datam) {
        
        byte[] data = new byte[datam.length];
        System.arraycopy(datam, 0, data, 0, datam.length);
        String tem_str = new String(datam,0,datam.length);
        tem_str = tem_str.trim();
        
        
        //System.out.println("The status is "+this.send_status);
        
        if(tem_str.equals("stop")){
            System.out.println("Stop okuttular bana dogru mu : "+tem_str);
            this.send_status = -1;
            try {
                this.save_video.finishWriting();
                this.save_video = null; // garbage collector here my friend
                this.trayman.statusMessage(); //show the tray mesage !
                
                this.currentMesage = "stop";
            } catch (IOException ex) {
                System.out.println("Error when closing the save stuff");
                
            }
        }
        
        else if(tem_str.equals("start")){
            //here you open your file really 
            System.out.println("Start okuttular bana dogru mu : "+tem_str);
            this.send_status=1;
            try {
                this.save_video = new SaveImage(); //create file writer
                this.currentMesage = "start";
            } catch (IOException ex) {
                
                System.out.println("Error when opening file to write");
            }
        }
        
        //if we continue recieve data
        if(this.send_status==1 && !tem_str.equals("start") && !tem_str.equals("stop")){
            //System.out.println("Continue okuttular bana dogru mu : ");
            try {
                save_video.writeThatByte(data, data.length);
                this.currentMesage = "continue";
            } catch (IOException ex) {
                System.out.println("Error when savng a chunk");
                
            }
            
        }
        
        return this.currentMesage;
        
    }

    
    public static void main(String[] args){
    
        try{
           TestServer ts = new TestServer(); 
           ts.start();
        }
        catch(Exception e){
            System.out.println("Some error in server");
          
        }
        
    }
}

    